function openPlayCourse(that,callback){
    var courseId = $(that).data('course-id');
    var catalogId = $(that).data('id');
    if(catalogId==null || typeof catalogId =='undefined'){
        catalogId = 0;
    }
    if (courseType == 'PACKAGE') {//套餐
        callback(courseId,0, 0);
    } else {//普通课程
        callback(0,courseId, catalogId);
    }
}

/**
 * 跳转播放页面
 */
function toPlayCourse(packageId,courseId, catalogId) {
    verifyPlay(packageId,courseId,catalogId);
}

/**
 * 跳转播放页面(立即学习)
 */
function goPlayCourse(packageId,courseId, catalogId) {
    window.location = baseloaction + '/web/uc/play/toPlayCourse?packageId='+packageId+'&courseId=' + courseId + '&catalogId=' + catalogId+'&type=LOBBY';
}


/**
 * 验证播放
 */
function verifyPlay(packageId,courseId, catalogId) {
    var prevCatalogId = getPrevCatalogId(catalogId,false);
    var  buyCourseId = courseId;
    if(packageId!=0){
        buyCourseId =packageId;
    }
    isLogin(function () {
        $.ajax({
            url: baseloaction +'/web/uc/play/checkPlay',
            type:'post',
            dataType:'json',
            data:{
                'buyCourseId' : buyCourseId,
                'courseId' : courseId,
                'prevCatalogId' : prevCatalogId,
                'catalogId' : catalogId
            },
            success:function (result) {
                if(result.success==true){
                    var entity = result.entity;
                    if(entity.play==true || entity.audition ==true){
                        window.location = baseloaction + '/web/uc/play/toPlayCourse?packageId='+packageId+'&courseId=' + courseId + '&catalogId=' + catalogId+'&type=LOBBY';
                    }else{
                        if(entity.needBuy!=undefined&&entity.needBuy!=null&&entity.needBuy==true){
                            guideBuy(buyCourseId,result.message);
                        }else{
                            icModale(0,'',result.message);
                        }
                    }
                }else{
                    icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，课程节点学习/播放失败');
            }
        });
    });
}


/**
 * 打开购买弹窗
 * @param courseId
 */
   function guideBuy(courseId,content){
    $.ajax({
        url:baseloaction + "/web/course/guideBuyCourse",
        type:'post',
        data:{courseId:courseId},
        dataType:"json",
        success:function(result){
            if(result.success){
                var btn = '';
                var callback;
                switch (result.entity) {
                    case 1:
                        content = '该课程为免费课程，建议免费报名，学习更多章节！';
                        btn = '免费报名';
                        callback = function (courseId){
                            genFreeOrder("FREE", courseId + '-COURSE-1', function () {
                                layer.close(index);
                            });
                        };
                        break;
                    case 2:
                        btn = '立即报名';
                        callback = function (courseId){
                            window.location.href = baseloaction +'/web/uc/order/initPayOrder?orderType=COMMON&shopData='+courseId+'-COURSE-1';
                        };
                        break;
                    case 3:
                        btn = '开通会员';
                        icModale(0,'',result.message);
                        callback = function (){
                            setTimeout(function(){
                                window.location.href = baseloaction +'/web/uc/member/queryMemberList';
                            },1000);
                        };
                        break;
                    default:
                        btn = '立即报名';
                        callback = function (courseId){
                            window.location.href = baseloaction +'/web/uc/order/initOrderPage?orderType=COMMON&shopData='+courseId+'-COURSE-1';
                        };
                        break;
                }

                var index =  layer.open({
                    type: 1,
                    title: '提示',
                    content: content,
                    btn: [btn, '我再想想 '],
                    area: 'auto',
                    btnAlign: 'c',
                    shade: [0.3, '#000000'],
                    skin: 'lay_ppComment__wrap',
                    yes: function (index) {
                        if($.isFunction(callback)){
                            callback(courseId);
                        }
                        layer.close(index);
                    },
                    btn2: function (index) {
                        layer.close(index);
                    }
                });
            }else{
                icModale(0,'','系统错误。跳转失败');
            }
        },error: function(){
            icModale(0,'','系统错误。跳转失败');
        }
    });
   }

/**
 * 获取前一个节点目录的ID
 * @param catalogId 当前获取目录的ID
 */
function getPrevCatalogId(catalogId,catalogClass) {
    var allCatalogList =$(".play-catalog-box");
    if(catalogClass){
        allCatalogList=$(".catalog-box");
    }

    var _index = -1;
    $.each(allCatalogList,function (index,em) {
        var _id = $(em).data("id");
        if(_id == catalogId){
            _index = index;
        }
    });
    if(_index > 0){
        var prev = allCatalogList[_index-1];
        return $(prev).data('id');
    }
    return 0;
}
